package com.budwk.app.sqfw.services.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.exception.Asserts;
import com.budwk.app.sqfw.dto.AssistanceDraftQueryDTO;
import com.budwk.app.sqfw.dto.AssistanceFormSummaryDTO;
import com.budwk.app.sqfw.dto.ThreeGuaranteeDTO;
import com.budwk.app.sqfw.services.AssistanceFormSummaryDraftService;
import com.budwk.app.sqfw.vo.ThreeGuaranteeVO;
import com.budwk.app.sqfwV2.dto.entity.AssistanceFormSummaryDraft;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author ZY.Wang
 * @since 2024/4/19
 */
@Slf4j
@IocBean(args = {"refer:dao"})
public class AssistanceFormSummaryDraftServiceImpl extends BaseServiceImpl<AssistanceFormSummaryDraft> implements AssistanceFormSummaryDraftService {
    public AssistanceFormSummaryDraftServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public void insertDraft(AssistanceFormSummaryDTO formSummaryDTO) {
        String userId = SecurityUtil.getUserId();
        if (StringUtils.isBlank(userId)) Asserts.fail("");

        AssistanceFormSummaryDraft preInsert = new AssistanceFormSummaryDraft();
        BeanUtil.copyProperties(formSummaryDTO, preInsert);
        preInsert.setCreateBy(userId);
        preInsert.setCreateDate(LocalDateTime.now());
        preInsert.setUpdateBy(userId);
        preInsert.setUpdateDate(LocalDateTime.now());
        insert(preInsert);
    }

    @Override
    public void updateDraft(AssistanceFormSummaryDTO formSummaryDTO) {
        if (StringUtils.isBlank(formSummaryDTO.getId())) Asserts.fail("更新数据缺少id");
        String userId = SecurityUtil.getUserId();

        AssistanceFormSummaryDraft queryDraft = fetch(formSummaryDTO.getId());
        BeanUtil.copyProperties(formSummaryDTO, queryDraft);
        queryDraft.setUpdateBy(userId);
        queryDraft.setUpdateDate(LocalDateTime.now());
        update(queryDraft);
    }

    @Override
    public List<ThreeGuaranteeVO> listUserDraft(String userId, AssistanceDraftQueryDTO queryDTO) {
        Cnd cnd = Cnd.NEW();
        cnd.and(AssistanceFormSummaryDraft::getCreateBy, "=", userId);

        if (StringUtils.isNotBlank(queryDTO.getType())) {
            cnd.and(AssistanceFormSummaryDraft::getType, "=", queryDTO.getType());
        }

        cnd.desc(AssistanceFormSummaryDraft::getCreateDate);
        List<AssistanceFormSummaryDraft> forSummaryDraftList = query(cnd);

        List<ThreeGuaranteeVO> resultList = Lists.newArrayList();
        for (AssistanceFormSummaryDraft formSummary : forSummaryDraftList) {
            ThreeGuaranteeVO vo = new ThreeGuaranteeVO();
            BeanUtil.copyProperties(formSummary, vo);

            JSONObject paramData = formSummary.getParamData();
            JSONObject paramDataJSON = paramData.getJSONObject("value");
            ThreeGuaranteeDTO threeGuaranteeDTO = paramDataJSON.toJavaObject(ThreeGuaranteeDTO.class);
            BeanUtil.copyProperties(threeGuaranteeDTO, vo);
            resultList.add(vo);
        }

        return resultList;
    }

    @Override
    public JSONObject detailDraft(String id) {
        AssistanceFormSummaryDraft draft = fetch(id);
        if (draft == null) Asserts.fail("数据不存在");

        ThreeGuaranteeVO vo = new ThreeGuaranteeVO();
        BeanUtil.copyProperties(draft, vo);

        JSONObject paramData = draft.getParamData();
        JSONObject paramDataJSON = paramData.getJSONObject("value");

        JSONObject resultJSON = JSONObject.parseObject(JSONObject.toJSONString(vo));
        resultJSON.putAll(paramDataJSON);

        return resultJSON;
    }

    @Override
    public void deleteDraft(String id) {
        delete(id);
    }
}
